<template>
  <el-form
    ref="ruleFormRef"
    :model="sortReportForm"
    :rules="rules"
    class="demo-ruleForm"
    label-position="top"
    size="small"
  >
    <!-- 物体标记号 -->
    <el-form-item
      label="物体标记号"
      prop="Index_Nr"
    >
      <el-input-number
        v-model="sortReportForm.Index_Nr"
        :min="0"
      />
    </el-form-item>

    <!-- 分配分拣站点 -->
    <el-form-item
      label="分配分拣站点"
      prop="Assign_Dest"
    >
      <el-input-number
        v-model="sortReportForm.Assign_Dest"
        :min="0"
      />
    </el-form-item>

    <!-- 额外分拣站点 -->
    <el-form-item
      label="额外分拣站点"
      prop="Extra_Dest"
    >
      <el-input-number
        v-model="sortReportForm.Extra_Dest"
        :min="0"
      />
    </el-form-item>

    <!-- 实际分拣站点 -->
    <el-form-item
      label="实际分拣站点"
      prop="Actual_Dest"
    >
      <el-input-number
        v-model="sortReportForm.Actual_Dest"
        :min="0"
      />
    </el-form-item>

    <!-- 设备编号 -->
    <el-form-item
      label="设备编号"
      prop="Unit_Nr"
    >
      <el-input-number
        v-model="sortReportForm.Unit_Nr"
        :min="0"
      />
    </el-form-item>

    <!-- 分拣结果 -->
    <el-form-item
      label="分拣结果"
      prop="Sort_Result"
    >
      <el-select
        v-model="sortReportForm.Sort_Result"
        placeholder="请选择分拣结果"
      >
        <el-option
          :value="0"
          label="PLC初始化消息"
        />
        <el-option
          :value="1"
          label="分拣成功"
        />
        <el-option
          :value="2"
          label="分拣失败"
        />
      </el-select>
    </el-form-item>

    <!-- 分拣消息 -->
    <el-form-item
      label="分拣消息"
      prop="Sort_Message"
    >
      <el-input-number
        v-model="sortReportForm.Sort_Message"
        :min="0"
      />
    </el-form-item>

    <!-- 标定设备号 -->
    <el-form-item
      label="标定设备号"
      prop="Calib_Unit"
    >
      <el-input-number
        v-model="sortReportForm.Calib_Unit"
        :min="0"
      />
    </el-form-item>

    <!-- 备用字段1~8 -->
    <el-form-item
      v-for="i in 8"
      :key="i"
      :label="`备用字段${i}`"
      :prop="`Spare${i}`"
    >
      <el-input-number
        v-model="sortReportForm[`Spare${i}`]"
        :min="0"
      />
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!pickServerStatus"
        :loading="loading"
      >
        发送
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        重置
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { sendMessageByServer } from '@/api/pick'
import { pickStore } from '@/store/pickStore'
import { storeToRefs } from 'pinia'

const store = pickStore()
const { pickServerStatus } = storeToRefs(store)

const loading = ref(false)
const ruleFormRef = ref<FormInstance>()

// 表单数据
const sortReportForm = reactive({
  Tel_Nr: 6,
  Index_Nr: 0,
  Assign_Dest: 0,
  Extra_Dest: 0,
  Actual_Dest: 0,
  Unit_Nr: 0,
  Sort_Result: 0,
  Sort_Message: 0,
  Calib_Unit: 0,
  Spare1: 0,
  Spare2: 0,
  Spare3: 0,
  Spare4: 0,
  Spare5: 0,
  Spare6: 0,
  Spare7: 0,
  Spare8: 0
})

// 校验规则
const rules = reactive<FormRules>({
  Index_Nr: [{ required: true, message: '请输入物体标记号', trigger: 'blur' }],
  Assign_Dest: [{ required: true, message: '请输入分配分拣站点', trigger: 'blur' }],
  Unit_Nr: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
  Sort_Result: [{ required: true, message: '请选择分拣结果', trigger: 'change' }]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(sortReportForm).then(res => {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'success',
          duration: 1000
        })
      }).finally(() => {
        loading.value = false
      })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
